namespace OasisCiq.Xml
{
    using System.Xml;
    using CommonTypes;
    using External;

    public class CommonTypesXmlWriter
    {
        private readonly XmlWriter writer;
        private const string XmlNS = XmlNamespaces.CommonTypes;

        public CommonTypesXmlWriter(XmlWriter writer)
        {
            this.writer = writer;
        }

        public void WriteExtendedAttributes(IExtendedAttributes element)
        {
            if (element == null || element.ExtendedAttributes == null)
            {
                return;
            }

            foreach (var attr in element.ExtendedAttributes.AllKeys)
            {
                foreach (var val in element.ExtendedAttributes.GetValues(attr))
                {
                    WriteExtendedAttribute(attr, val);
                }
            }
        }

        public void WriteExtendedAttribute(string name, string value)
        {
            var ns = string.Empty;
            var localName = name;
            if (name.StartsWith("{"))
            {
                var parts = localName.Substring(1).Split('}');
                ns = parts[0];
                localName = parts[1];
            }

            writer.WriteAttributeString(localName, ns, value);
        }

        public void WriteValidityDateGroup(IValidityDateGroup element)
        {
            if (element == null)
            {
                return;
            }

            if (element.DateValidFrom != null)
            {
                writer.WriteAttributeString("DateValidFrom", XmlNS, element.DateValidFrom.ToString());
            }

            if (element.DateValidTo != null)
            {
                writer.WriteAttributeString("DateValidTo", XmlNS, element.DateValidTo.ToString());
            }
        }

        public void WriteAbbreviationGroup(IAbbreviationGroup element)
        {
            if (element == null || element.Abbreviation == null)
            {
                return;
            }

            writer.WriteAttributeString("Abbreviation", XmlNS, element.Abbreviation.ToString());
        }

        public void WriteDataQualityGroup(IDataQualityGroup element)
        {
            if (element == null)
            {
                return;
            }

            if (element.DataQualityType != null)
            {
                writer.WriteAttributeString("DataQualityType", XmlNS, element.DataQualityType.ToString());
            }

            if (element.ValidFrom != null)
            {
                writer.WriteAttributeString("ValidFrom", XmlNS, element.ValidFrom.ToString());
            }

            if (element.ValidTo != null)
            {
                writer.WriteAttributeString("ValidTo", XmlNS, element.ValidTo.ToString());
            }
        }

        public void WriteLanguageCodeGroup(ILanguageCodeGroup element)
        {
            if (element == null || element.LanguageCode == null)
            {
                return;
            }

            writer.WriteAttributeString("LanguageCode", XmlNS, element.LanguageCode.ToString());
        }

        public void WriteStatusList(StatusList status, string ns)
        {
            if (status != null)
            {
                writer.WriteAttributeString("Status", ns, status.ToString());
            }
        }
    }
}